<#--
    - List of variables:
        +- Package: the package name of entity classes.
        |
        +- PackageDirectory
        |
        +- Classes:
            |
            +- name: The name of the class.
            |
            +- fields: the fields in the class. Also this shuld contain:
            |    |
            |    +- name
            |    |
            |    +- type
            |
            +- relationFields: the fields in the class. Also this shuld contain:
            |    |
            |    +- name
            |    |
            |    +- type
            |
            +- allFields:
                 |
                 +- name
                 |
                 +- type

-->
<@pp.dropOutputFile />
<#list Classes as Class>
<@pp.changeOutputFile name="/src/${PackageDirectory}"+"/"+"${Class.name}"+".java" />

package ${Package};
import java.io.Serializable;
import java.sql.Date;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Query;
import javax.persistence.Table;
import javax.persistence.JoinColumn;


/**
 * Generated Code - Do NOT Edit - For now ;)
 */

@Table(name = "${Class.name}")
@Entity
public class ${Class.name} extends Controller implements Serializable
{
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id", nullable = false)
    private Integer id;

    <#list Class.fields as field>
    @Column(name = "${field.name}")
    private ${field.type} ${field.name};

    </#list>

    <#list Class.relationFields as relationFields>
    @JoinColumn
    private ${relationFields.type} ${relationFields.name};

    </#list>


    public ${Class.name}()
    {
    }
    public ${Class.name}(<#list Class.allFields as field>${field.type} ${field.name}<#if field_has_next>, </#if></#list>)
    {
        <#list Class.allFields as field>
        this.${field.name} = ${field.name};
        </#list>
    }

    //Getters & Setters
    public Integer getId()
    {
        return id;
    }

    public void setId(Integer id)
    {
        this.id = id;
    }

    <#list Class.allFields as field>

    public ${field.type} get${field.name?cap_first}()
    {
        return ${field.name};
    }

    public void set${field.name?cap_first}(${field.type} ${field.name})
    {
        this.${field.name} = ${field.name};
    }
    </#list>

    @Override
    public int hashCode()
    {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object)
    {
        boolean result = false;
        if (!(object instanceof ${Class.name}))
        {
            return false;
        }

        ${Class.name} other = (${Class.name}) object;

        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id)))
        {
            return false;
        }

        if (<#list Class.allFields as field>this.${field.name}.equals(other.${field.name}) <#if field_has_next>&&</#if>
            </#list>)
        {
            if (this.id == null && other.id == null)
            {
                return true;
            }
            else if (this.id.equals(other.id))
            {
                return true;
            }
        }
        return false;
    }
    @Override
    public String toString()
    {
        return "${Package}.${Class.name}[id=" + id + "]";
    }

    public final static class Select
    {

        public final static List<${Class.name}> All()
        {

            return Select.All(true, -1, -1);
        }

        public final static List<${Class.name}>All(int maxResults, int firstResult)
        {
            return Select.All(false, maxResults, firstResult);
        }

        private final static List<${Class.name}>All(boolean all, int maxResults, int firstResult)
        {
            EntityManager em = DatabaseEntityManager.getEntityManager();
            try
            {
                Query q = em.createQuery("select object(o) from ${Class.name} as o");
                if (!all)
                {
                    q.setMaxResults(maxResults);
                    q.setFirstResult(firstResult);
                }
                return q.getResultList();
            } finally
            {
                em.close();
            }
        }

        public final static List<${Class.name}> Where(String jqlWhere)
        {
           EntityManager em = DatabaseEntityManager.getEntityManager();
            try
            {
                Query q = em.createQuery("select object(${Class.name}) from ${Class.name} as ${Class.name} where " + jqlWhere);
                return q.getResultList();
            } finally
            {
                em.close();
            }
        }
    }
}
</#list>